<template>
  <el-dialog
    :title="!dataForm.postId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="所属部门" prop="departId">
        <el-select v-model="dataForm.departId" placeholder="请选择">
          <el-option v-for="(item,i) in departModel" :label="item.departName" :value="item.departId"></el-option>
        </el-select>
      </el-form-item>
    <el-form-item label="岗位名称" prop="postName">
      <el-input v-model="dataForm.postName" placeholder="岗位名称"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    created () {
      this.getDepart()
    },
    data () {
      return {
        visible: false,
        dataForm: {
          postId: 0,
          departId: '',
          postName: '',
          createTime: '',
          updateTime: ''
        },
        dataRule: {
          departId: [
            { required: true, message: '部门编号不能为空', trigger: 'blur' }
          ],
          postName: [
            { required: true, message: '岗位名称不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ]
        },
        departModel: {
          departId: '',
          departName: ''
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.postId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.postId) {
            this.$http({
              url: this.$http.adornUrl(`/tb_bs_post/bspost/info/${this.dataForm.postId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.departId = data.bsPost.departId
                this.dataForm.postName = data.bsPost.postName
                this.dataForm.createTime = data.bsPost.createTime
                this.dataForm.updateTime = data.bsPost.updateTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/tb_bs_post/bspost/${!this.dataForm.postId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'postId': this.dataForm.postId || undefined,
                'departId': this.dataForm.departId,
                'postName': this.dataForm.postName,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      getDepart () {
        this.$http({
          url: this.$http.adornUrl(`/tb_bs_post/bspost/getDepart`),
          method: 'get'
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.departModel = data.date
            console.log(data.date)
          }
        })
      }
    }
  }
</script>
